package com.projectnection.server.webservices;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.projectnection.server.database.entities.GetEntity;
import com.projectnection.server.database.entities.SaveEntity;
import com.projectnection.server.model.Entity;

@SuppressWarnings("serial")
public class SaveEntityServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		String entityType = request.getParameter("entityType");

		String entityID = request.getParameter("entityID");

		String entityName = request.getParameter("entityName");

		Entity entity = null;

		if (entityType == null) {

			entity = GetEntity.get(entityID);
		} else {

			entity = new Entity();

			entity.setEntityType(entityType);
			entity.setEntityID(entityID);
		}

		entity.setEntityName(entityName);

		SaveEntity.save(entity);

		String answer = Utilities.getCallbackResponse(request
				.getParameter("callback"));

		response.setContentType("text/javascript");

		response.getWriter().println(answer);
	}
}
